{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd5253bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#概念\n",
    "#列表是动态的，长度大小不固定，可以随意地增加、删减或者改变元素（mutable）。\n",
    "#而元组是静态的，长度大小固定，无法增加删减或者改变（immutable）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "acd27c77-e57f-4dc9-a6e4-570c82a008e8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "4\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(tup[\u001b[38;5;241m3\u001b[39m])\n\u001b[0;32m      6\u001b[0m l[\u001b[38;5;241m3\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m40\u001b[39m\n\u001b[1;32m----> 7\u001b[0m \u001b[43mtup\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m40\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 示例\n",
    "l = [1,2,3,4]\n",
    "tup = (1,2,3,4) \n",
    "print(l[3])\n",
    "print(tup[3])\n",
    "l[3] = 40\n",
    "tup[3] = 40  # tuple不可以赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "329d641b-6dd6-4675-b652-fceea33b65b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3, 4, 5)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表以及元组添加内容\n",
    "new_tup = tup+(5,)\n",
    "new_tup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1d153bcf-d8e8-4c0a-8b9f-3d68a3cc2c91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n",
      "4\n",
      "[2, 3]\n",
      "(2, 3)\n"
     ]
    }
   ],
   "source": [
    "# 索引以及切片\n",
    "print(l[-1])\n",
    "print(tup[-1])\n",
    "print(l[1:3] ) \n",
    "print(tup[1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "51949e97-01e8-4a8e-be93-c8e436de68da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5]]\n",
      "((1, 2, 3), (4, 5))\n"
     ]
    }
   ],
   "source": [
    "# 嵌套使用\n",
    "l = [[1,2,3],[4,5]]\n",
    "tup = ((1,2,3),(4,5)) \n",
    "print(l)\n",
    "print(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0f48d653-1fb3-4822-8000-409cc8d5da49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "(1, 2, 3, 4)\n"
     ]
    }
   ],
   "source": [
    "# 实现列表 和元组的转换,list列表有reverse和sort函数\n",
    "# 元组需要用到revesed()和sorted）进行迭代器转换\n",
    "print(list((1,2,3,4)))\n",
    "print(tuple([1,2,3,4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a3abd71e-2ca3-4f41-a420-80c8bbb13095",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 常用函数\n",
    "l = [3,2,3,7,8,1] \n",
    "l.count(3)\n",
    "l.index(7)\n",
    "l.reverse()\n",
    "l\n",
    "l.sort() \n",
    "l\n",
    "l.index(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0dcd9ef0-18f0-4ba1-b0ea-c4da82cea3c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 3, 7, 8]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = (3,2,3,7,8,1) \n",
    "tup.count(3)\n",
    "tup.index(7) \n",
    "list(reversed(tup))\n",
    "sorted(tup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4898fee1-d4cc-423a-bf97-199c885c3600",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
